
source('code/analysis/load.R')

defund = read_dta('data/working/defund-sender.dta') %>%
  mutate(exp = 'defund') %>% 
  bind_rows(read_dta('data/working/defund-sender-replication.dta') %>% mutate(exp = 'defund-replication', ind =0)) %>% 
  data_finalize() %>%
  filter(join==1) %>%
  mutate(age_group = case_when(age >= 18 & age <= 29 ~ 1,
                               age >= 30 & age <= 49 ~ 2,
                               age >= 50 & age <= 64 ~ 3,
                               age >= 65 ~ 4)) %>%
  drop_na(age_group, black, asian, white, hisp, male, hs, bachelors)

deport = read_dta('data/working/deport-sender.dta') %>%
  mutate(exp = 'deport') %>% 
  data_finalize() %>%
  filter(join==1) %>%
  mutate(age_group = case_when(age >= 18 & age <= 29 ~ 1,
                               age >= 30 & age <= 49 ~ 2,
                               age >= 50 & age <= 64 ~ 3,
                               age >= 65 ~ 4)) %>%
  drop_na(age_group, black, asian, white, hisp, male, hs, bachelors)




get_weight = function(data) {

  
  if (data$exp[1] =='defund') {
    party=2
  }
  if (data$exp[1] =='deport') {
    party=1
  }
  pew = read_dta('data/working/pew.dta')
  pew_exp = pew %>% filter(political_party_lean==party) %>%
    mutate(ind = as.numeric(political_party == 3),
           age_group = case_when(age == 23.5 ~ 1,
                                 age == 39.5 ~ 2,
                                 age == 57 ~ 3,
                                 age == 65 ~ 4)) %>%
    drop_na(age_group, black, asian, white, hisp, male, hs, bachelors)
  
  # factor for raking
  cols = c('age_group', 'black', 'asian', 'white', 'hisp', 'male', 'hs', 'bachelors')
  data[cols] = lapply(data[cols], factor)  
  pew_exp[cols] = lapply(pew_exp[cols], factor)  
  
  # target sample: pew_exp
  # main sample: data
  
  # create the target weights for covariates
  age_w= wpct(pew_exp$age_group)
  black_w= wpct(pew_exp$black)
  asian_w = wpct(pew_exp$asian)
  white_w = wpct(pew_exp$white)
  hisp_w = wpct(pew_exp$hisp)
  male_w = wpct(pew_exp$male)
  highschool_w = wpct(pew_exp$hs)
  bachelor_w = wpct(pew_exp$bachelors)
  
  targets = list(age_w, black_w, asian_w, white_w, hisp_w, male_w, highschool_w, bachelor_w)
  names(targets) = c('age_group', 'black', 'asian', 'white', 'hisp', 'male', 'hs', 'bachelors')
  
  data$caseid = 1:length(data$age_group)
  data = as.data.frame(data)
  
  anesrakefinder(targets, data, choosemethod = "total")
  
  # weighting process
  match = anesrake(targets, data, caseid = data$caseid,
                   verbose= FALSE, choosemethod = "total",
                   iterate = TRUE , force1 = TRUE)
  
  # create the weights
  data$weight = unlist(match[1])
  
  return(data)

}

defund = get_weight(defund)
deport = get_weight(deport)



main_results_reweight = function() {
  
  title = 'Willingness to post anti-defunding or pro-deportation Tweet (reweighted estimates)'
  depvar = '\\textit{Scheduled Tweet}'
  label = 'sender-reweight'
  
  ## Panel A: without controls
  model1 = lm(posted~excuse, data=defund, weights=weight)
  model2 = lm(posted~excuse+age+I(age^2)+race+hisp+male+education, data=defund, weights=weight)
  model3 = lm(posted~excuse, data=deport, weights=weight)
  model4 = lm(posted~excuse+age+I(age^2)+race+hisp+male+education,data=deport, weights=weight)

  mean =   paste(unlist(formatC(c(mean(defund$posted[defund$excuse==0], na.rm = T),
                                  mean(defund$posted[defund$excuse==0], na.rm = T),
                                  mean(deport$posted[deport$excuse==0], na.rm = T),
                                  mean(deport$posted[deport$excuse==0], na.rm = T)), digits=3)), collapse=' & ')
  
  out = stargazer(list(model1, model2, model3, model4), 
                  omit='age|race|hisp|male|rep|education|Constant|partisan',
                  covariate.labels = c('Cover'),
                  keep.stat = c('rsq','n'), dep.var.labels = depvar,
                  column.labels = c('Experiment 1', 'Experiment 3'),
                  column.separate = c(2,2),
                  title = title, label = str_interp('t:${label}'),
                  add.lines = list(c('Controls', rep(c('No', 'Yes'),2))))
  
  means = str_interp('No Cover mean & ${mean}  \\\\')
  
  out[13] = paste0(out[13], '\\cmidrule(rr){2-3} \\cmidrule(rr){4-5}')
  out = c(out[1:8], out[11:19], means, out[20:length(out)])
  out = star_notes_tex(out, note.type = 'threeparttable', note = tablenotes[["reweight"]])
  
  writeLines(out, con = str_interp('output/tables/${label}.tex'))
  
}


main_results_reweight()






